<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
	<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
	<title>课程介绍</title>
	<link rel="stylesheet" type="text/css" href="../../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../../css/weui.css" />
	<link rel="stylesheet" type="text/css" href="../../css/exceptions.css" />
    <style type="text/css">
	    /*简介*/
    	#courseDescription{
    		font-size: 14px;
    		padding: 10px;
    		color: #131316;
    	}
    	hr{
    		height: 1px;
    		background-color: #d9d9d9;
    		margin-bottom: 10px;
    	}
    	/*显示评论人数*/
    	.commentPersonNumDiv{
    		margin: 10px;
    	}
    	#publishComment{
    		float: right;
    		display: none;
    	}
    	/*评论列表*/
    	.touxiang{
            width: 38px;
            height: 38px;
            border-radius: 3em;
        }
        .starImg{
    		width: 20px;
    		height: 20px;
    		margin-left: 5px;
    	}
        ul{
            padding-left: 10px;
            padding-right: 10px;
        }
        li{
            margin-top: 10px;
            list-style: none;
            margin-left: 0px;
            padding-bottom: 8px;
            border-bottom: 1px solid #d9d9d9;
        }
        .nameLabel{
            margin-top: 9px;
            margin-left: 8px;
            position: absolute;
            font-size: 14px;
            color: #444;
        }
        .commentListDiv table{
            margin-top: -25px;
            margin-left: 43px;
        }
        .contentDiv{
            margin-left: 46px;
            font-size: 14px;
            /*text-overflow: -o-ellipsis-lastline;
            overflow: hidden;*/
          /*  text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;*/
            color: #444;
        }
        .timeSpan{
            display: block;
            margin-top: 5px;
            margin-left: 46px;
            color: #C4C4C4;
            font-size: 12px;
            width: 130px;
        }
        .deleteSpan,.replySpan{
        	display: block;
            margin-top: -20px;
            /*margin-left: 46px;*/
            color: #C4C4C4;
            font-size: 12px;
            width: 30px;
            position: absolute;
            text-align: center;
        }
        .hiddenUidSpan{
        	display: none;
        }
        .commentAbnormal{
        	top: 60px;
        }
        .replyTagSpan{
        	color: #09bb07;
        }
    </style>
</head>
<body>
	<!-- <div class="noSign noDataDiv commentAbnormal" hidden="hidden" id="noData">
		<img src="../../image/icon_img.png" width="60%">
		<p class="font16 top_15 base_dark_grey" >
			暂无数据
		</p>
	</div>
	<div class="loadFailure noInternetDiv commentAbnormal" hidden="hidden" id="noNet">
		<img src="../../image/icon_img01.png" width="60%">
		<p class="" >
			数据加载失败
		</p>
		<p>
			请检查您的网络
		</p>
		<button type="button" class="mui-btn mui-btn-outlined" onclick="loadData()">
			重新加载
		</button>
	</div> -->
	<!-- 课程简介 -->
	<div class="courseDescriptionDiv">
		<p id="courseDescription">
		</p>
	</div>
	<!-- <hr/> -->
	<!-- <div class="commentPersonNumDiv">
		<span>全部评论（<span id="commAllNum"></span>）</span>
		<a href="javascript:;" class="weui_btn weui_btn_mini weui_btn_primary" id="publishComment">发布评论</a>
	</div>
	<hr/>
	<div class="commentListDiv">
		<ul class="commentListUl">
			
		</ul>
	</div> -->
	<!-- 是否删除提示框 -->
    <!-- <div class="weui_dialog_confirm deleteAlertDiv" style="display: none">
        <div class="weui_mask"></div>
            <div class="weui_dialog">
                <div class="weui_dialog_hd">
                  <strong class="weui_dialog_title">提示</strong>
                </div>
                <div class="weui_dialog_bd">是否删除评论<br>...</div>
                <div class="weui_dialog_ft">
                    <a href="javascript:;" class="weui_btn_dialog default deleteCancel">取消</a>
                    <a href="javascript:;" class="weui_btn_dialog primary deleteSure">确定</a>
                </div>
            </div>
    </div> -->
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/databaseOperation.js"></script>
<script type="text/javascript" src="../../script/url.js"></script>
<script type="text/javascript">
	var page = 1;
	var pagerow = 10;
	var commentNameId = '';
    var commentContentId = '';
    var commentTime = '';
    var commentImage = '';
    var commentId = ''; 
    var commentUid = '';
    var isSelectType ;
	DBReady = function(){
//	    var description = api.pageParam.description;
	
//	    $api.text($api.byId('courseDescription'), description);

	    openDB(function(ret, err){
	    	if (ret.status) {
	    		var sql = "SELECT courseinfo FROM t_study_course_info WHERE courseid=" + api.pageParam.cid + ";";
	    		selectSQL(sql, function(ret, err){
	    			if (ret.status && ret.data[0].courseinfo) {
	    				$api.text($api.byId('courseDescription'), ret.data[0].courseinfo);
	    			}else{
	    				$api.text($api.byId('courseDescription'), '暂无简介');
	    			}
	    			// 修改暂无数据或无网络位置
    				var noContentTop = $('#courseDescription').height()+50;
    				$('.commentAbnormal').css('top',noContentTop);
	    		});
	    	}
	    });
		// 是否显示无网络或无数据
		// unknown            //未知
		// ethernet        //以太网
		// wifi            //wifi
		// 2g                //2G网络
		// 3g                //3G网络
		// 4g                //4G网络
		// none            //无网络
		loadDataFailShowPageFun = function(data){
			var connectionType = api.connectionType;
			if(connectionType != 'none'){
				$api.css($api.dom('.noInternetDiv'), 'display:none;');
				if(data.length == 0){
					$api.css($api.dom('.noDataDiv'), 'display:block;');
				}
				else{
					$api.css($api.dom('.noDataDiv'), 'display:none;');
				}
			}
			else{
				$api.css($api.dom('.noDataDiv'), 'display:none;');
				if(data.length == 0){
					$api.css($api.dom('.noInternetDiv'), 'display:block;');
				}
				else{
					$api.css($api.dom('.noInternetDiv'), 'display:none;');
				}
			}
		}


		// 接收参数
	    var getParam = api.pageParam;
		// 
		// 
		// 
		// 根据课程是否已选修改是否可发布评论
	    alterSelectTypeFun = function(){
	    	if(isSelectType == '0'){
	    	
		    	$api.css($api.dom('#publishComment'), 'display: none;');
		    }
		    else{
		     
		    $api.css($api.dom('#publishComment'), 'display: block;');
		    	// 已选修，可发布评论
		    	$api.addEvt($api.dom('#publishComment'), 'click', function(event){
		    
		    		event.stopPropagation();
		    		api.openFrame({
		    		    name: 'study_commentEdit',
		    		    url: 'study_commentEdit.html',
		    		    bounces: false,
		    		    rect: {
		    		    	x: 0,
		    		    	y: 0,
		    		    	w: 'auto',
		    		    	h: 'auto'
		    		    },
		    		    reload: true,
		    		    pageParam:{
		    		    	cid:getParam.cid,
		    		    	title:getParam.description,
		    		    	reply:'false',
		    		    	source:getParam.source,
		    		    	part: getParam.part
		    		    }
		    		});
		        
		    	
		    	});
		    }
	    }
	    // 接收type，判断是否已选修，选修后隐藏评论列表
	    // var isSelectType = api.pageParam;
	    // 
	    // 
	    

	    isSelectType = getParam.selected;
	    // alterSelectTypeFun();

	    api.addEventListener({
 	        name: 'selectedCourseNotifi'
 	    }, function(ret, err){
 	        isSelectType = ret.value.selected;
 	        alterSelectTypeFun();
 	        if(isSelectType == '0'){
 	        	$('.deleteSpan,.replySpan').hide();
 	        }
 	        else{
 	        	$('.deleteSpan,.replySpan').show();
 	        	$('.deleteSpan').css('margin-left',api.frameWidth-20-30-5-30);
            	$('.replySpan').css('margin-left',api.frameWidth-20-30);
 	        }
 	    });
	    api.addEventListener({
		    name: 'filesDownloadFinishNotifi'
		}, function(ret, err){
		});
		api.addEventListener({
		    name: 'downlaodFailedNotifi'
		}, function(ret, err){
		});
		api.addEventListener({
            name: 'allChapterStudiedNotifi'
        }, function(ret, err){
        });
        api.addEventListener({
            name: 'studyPassedExamNotifi'
        }, function(ret, err){
        });
	    // 接收列表星星的个数
	    loadStarNum = function(num){
	        var starTd='';
	        switch(num){
	            case '1':
	                starTd='<td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td>';
	                break;
	            case '2':
	                starTd='<td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td>';
	                break;
	            case '3':
	                starTd='<td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td>';
	                break;
	            case '4':
	                starTd='<td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing1.png" alt=""></td>';
	                break;
	            case '5':
	                starTd='<td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td><td><img class="starImg" src="../../image/icon_xingxing.png" alt=""></td>';
	                break;
	        }
	        return starTd;
	    }
	    selectScore = function(liId){
	        openDB(function(ret, err){
	            if (ret.status) {
	                var selectScoreSql = 'select score from t_study_comment where commentid='+liId;
	                selectSQL(selectScoreSql,function(ret, err){
	                    if(ret.status){
	                        var data = ret.data;
	                        starCountComment = data[0].score;
	                        api.openWin({useWKWebView:true,
	                            name: 'study_commentReply',
	                            url: 'study_commentReply.html',
	                            bounces: false,
	                            reload: true,
	                            pageParam:{
	                                commentId:commentId,
	                                commentName:$(commentNameId).text(),
	                                commentContent:$(commentContentId).text(),
	                                commentTime:$(commentTime).text(),
	                                commentImage:$(commentImage).attr('src'),
	                                commentStarNum:starCountComment,
	                                commentUid:$(commentUid).text()
	                            }
	                        });
	                    }
	                    else{
//	                   
	                    }
	                   
	                });
	            }
	        });
	    }

	    loadData = function(){
	        // 从数据库中查询数据
	        // comment,uid,ctime,score,cid,commentid,uimage,uname
	        openDB(function(ret, err){
	            if (ret.status) {
	                var selectScoreSql = 'select * from t_study_comment where cid='+getParam.cid +' order by commentid DESC' +' limit ' + (page-1)*pagerow + ',' + pagerow ;
	                selectSQL(selectScoreSql,function(ret, err){
	                    if (ret.status) {
	                        var data = ret.data;
	                        setPageContentFun(data,'true');
	                    }
	                    
	                    
	                });
	            }
	        });
	    }
	    setPageContentFun = function(data,cache){
	    	var starCountComment = 0;
	    	var pageListStr = '';
            for (var i = 0; i < data.length; i++) {
                if(data[i].id){
                	var commentId;
                	var comUimage;
                	var comUname;
                	var comCtime;
                	var comTo_uname;
                	var showReplyNameStr = '';

                	if(cache == 'true'){
                		commentId = data[i].commentid;
                		comUimage = data[i].uimage;
                		comUname = data[i].uname;
                		comCtime = data[i].ctime;
                		comTo_uname = data[i].to_uname;
                	}
                	else{
                		commentId = data[i].id;
                		comUimage = data[i].picurl;
                		comUname = data[i].uname;
                		comCtime = getCurrentDate(data[i].ctime);
                		comTo_uname = data[i].to_uname;
                	}
                	// 是否显示删除
                    var deleteStr = '';
                    if(data[i].uid == $api.getStorage('uid')){
                    	deleteStr = '<span class="deleteSpan" id=delete'+commentId+'>'+'删除'+'</span>';
                    }
                	// 判断是否有头像
                	var patt1=new RegExp("http://");
					if (!patt1.test(comUimage)){
						comUimage = '../../image/disImage@2x.png';
					}
					if(comTo_uname.length > 0){
						showReplyNameStr = '回复<span class="replyTagSpan">@'+comTo_uname+':&nbsp;&nbsp;</span>';
					}
					pageListStr += '<li class="comList" id='+commentId+'><img class="touxiang" id=commentImage'+commentId+' src='+comUimage+' alt=""><label class="nameLabel" id=name'+commentId+'>'+comUname+'</label><!--<table><tbody><tr>'+loadStarNum(data[i].score)+'</tr></tbody></table> --><div class="contentDiv" id=content'+commentId+'>'+ showReplyNameStr +data[i].comment+'</div><span class="timeSpan" id=time'+commentId+'>'+comCtime+'</span><span class="hiddenUidSpan" id=hideUid'+commentId+'>'+data[i].uid+'</span>'+deleteStr+'<span class="replySpan" id=reply'+commentId+'>'+'回复'+'</span></li>';
                }
            }
            if(page == 1){
            	loadDataFailShowPageFun(data);
            	$('.commentListUl').html(pageListStr);
            }
            else{
            	$('.commentListUl').append(pageListStr);
            }
            $('.comList').unbind('click');
            $('.deleteSpan').unbind('click');
            $('.replySpan').unbind('click');
            selectCommAllNum();
            // $api.text($api.dom('#commAllNum'), data.length);
            if(isSelectType == '1'){
            	$('.replySpan').show();
            	$('.deleteSpan').css('margin-left',api.frameWidth-20-30-5-30);
            	$('.replySpan').css('margin-left',api.frameWidth-20-30);
            	// $('.comList').click(function(event){
                //     event.stopPropagation();
                //     commentId = $(this).attr('id');
                //     commentNameId='#name'+$(this).attr('id');
                //     commentContentId='#content'+$(this).attr('id');
                //     commentTime='#time'+$(this).attr('id');
                //     commentImage='#commentImage'+$(this).attr('id');
                //     commentUid='#hideUid'+$(this).attr('id')
                //     //从数据库中查询当前点击行的星星数量
                //     selectScore($(this).attr('id'));
                // });
                $('.deleteSpan').click(function(event){
                	event.stopPropagation();
                	// 
                	// 添加删除提示
                	// $api.css($api.dom('.deleteAlertDiv'), 'display:block;');
                	// $api.attr($api.dom('.deleteAlertDiv'), 'id', 'alert'+$(this).attr('id'));
                	// 添加删除提示end
                	// 
                	// 
                	// 
                	var deleteCommentId = $(this).attr('id').substring(6,$(this).attr('id').length);
                	deleteCommFun(deleteCommentId);
                })

                $('.replySpan').click(function(event){
                	event.stopPropagation();
                	// 
                	// 添加删除提示
                	// $api.css($api.dom('.deleteAlertDiv'), 'display:block;');
                	// $api.attr($api.dom('.deleteAlertDiv'), 'id', 'alert'+$(this).attr('id'));
                	// 添加删除提示end
                	// 
                	// 
                	// 
                	var replyCommentId = $(this).attr('id').substring(5,$(this).attr('id').length);
               
                	replyCommentFun(replyCommentId);
                })
            }
            else{
            	$('.deleteSpan,.replySpan').hide();
            }
	    }
	    $api.addEvt($api.dom('.deleteCancel'), 'click', function(){
            $api.css($api.dom('.deleteAlertDiv'), 'display:none;');
        });
        $api.addEvt($api.dom('.deleteSure'), 'click', function(){
            $api.css($api.dom('.deleteAlertDiv'), 'display:none;');
            var deleteCommentId = $api.attr($api.dom('.deleteAlertDiv'), 'id').substring(11,$api.attr($api.dom('.deleteAlertDiv'), 'id').length);
            deleteCommFun(deleteCommentId);
        
            
        });
        // 回复评论
        replyCommentFun = function(replyCommentId){
        	event.stopPropagation();
    		api.openFrame({
    		    name: 'study_commentEdit',
    		    url: 'study_commentEdit.html',
    		    bounces: false,
    		    rect: {
    		    	x: 0,
    		    	y: 0,
    		    	w: 'auto',
    		    	h: 'auto'
    		    },
    		    reload: true,
    		    pageParam:{
    		    	cid:replyCommentId,
    		    	title:getParam.description,
    		    	reply:'true',
    		    	part: getParam.part
    		    }
    		});
        }
        // 在此页面添加评论删除
        deleteCommFun = function(deleteCommentId){
        	var deleteComParam = {
        		id: deleteCommentId,
	     		jid:$api.getStorage('jid'),
	     		uid: $api.getStorage('uid'),
	     		part: getParam.part
             
        	}
        	api.ajax({
        	    url: Study_DeleteComment,
        	    method: 'post',
        	    timeout: 30,
        	    dataType: 'json',
        	    returnAll:false,
        	    data:{
        	    	values:deleteComParam
        	    }
        	},function(ret,err){
        	    if (ret) {
        	        // TODO 
        	    
        	        openDB(function(ret, err){
		                if (ret.status) {
		                	var deleteSqlReply = 'delete from t_study_comment_reply where commentid='+deleteCommentId;
		                    executeSQL(deleteSqlReply,function(ret, err){
		                        if(ret.status){
		                            
		                        }
		                        else{
		                            
		                        }
		                    });
		                    var deleteSql = 'delete from t_study_comment where commentid='+deleteCommentId;
		                    executeSQL(deleteSql,function(ret, err){
		                        if(ret.status){
		                            // 删除成功后，同时删除列表中的数据
		                            var currentDeleteCommentLiId='#'+deleteCommentId;
		                            $('.commentListUl '+currentDeleteCommentLiId).remove();
		                            loadDataFailShowPageFun($(".commentListUl li"));
		                        }
		                        else{
		                           
		                        }
		                
		                   
		                                selectCommAllNum();
		                      
		                    });
		                }
		            });
        	    } else {
        	    
        	        if(err.statusCode == 0){
                        api.toast({
                            msg: '请连接网络',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
        	    };
        	});
        }
        // 当前时间，年－月－日 时：分：秒
        getCurrentDate = function(currentTime){
            var dateTimeString='';//将时间格式化为如:2016-02-12 12:23:01
			
            var mydate = new Date();
            mydate.setTime(currentTime * 1000);
            var getYearStr = mydate.getFullYear(); //获取完整的年份(4位,1970-????)
            var getMonthStr = mydate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            // 当月份小于10时，在前面加0
            if(getMonthStr <10){
                getMonthStr = '0'+getMonthStr;
            }
            var getDateStr = mydate.getDate(); //获取当前日(1-31)
            // 当天数小于10时，在前面加0
            if(getDateStr <10){
                getDateStr = '0'+getDateStr;
            }
            var getHoursStr = mydate.getHours(); //获取当前小时数(0-23)
            // 当时数小于10时，在前面加0
            if(getHoursStr <10){
                getHoursStr = '0'+getHoursStr;
            }
            var getMinutesStr = mydate.getMinutes(); //获取当前分钟数(0-59)
            // 当分钟小于10时，在前面加0
            if(getMinutesStr <10){
                getMinutesStr = '0'+getMinutesStr;
            }
            var getSecondsStr = mydate.getSeconds(); //获取当前秒数(0-59)
            // 当秒数小于10时，在前面加0
            if(getSecondsStr <10){
                getSecondsStr = '0'+getSecondsStr;
            }
            // mydate.toLocaleDateString(); //获取当前日期 2016/01/01
            // mydate.toLocaleTimeString(); //获取当前时间 AM09:12:12
            // mydate.toLocaleString(); //获取日期与时间  2016/01/01 AM09:12:12
            dateTimeString = getYearStr + '-' + getMonthStr + '-' + getDateStr + ' ' + getHoursStr + ':' + getMinutesStr + ':' + getSecondsStr;
            return dateTimeString;
        }
        // 获得请求数据，插入数据库
		// // 防止只插入最后一条语句
		insertCommentDataCallBackFun = function(insertStr,callBack){
			openDB(function(ret,err){
				if(ret.status){
					callBack(insertStr);
				}
			})
		}
		insertCommentData = function (dataArr) {
			// body...
			// 插入数据成功，查询  t_study_course_data
            // openDB(function(ret,err){
                // if(ret.status){
                    var j = 0;
                    if(dataArr.length > 0){
                    	for (var i = 0; i < dataArr.length; i++) {
	                        var insertCommentParam={
	                            uid : dataArr[i].uid,
				                cid : getParam.cid,
				                ctime : getCurrentDate(dataArr[i].ctime),
				                comment : dataArr[i].comment,
				                commentid : dataArr[i].id,
				                score : dataArr[i].score,
				                uimage : dataArr[i].picurl,
				                uname : dataArr[i].uname,
				                to_uname: dataArr[i].to_uname
	                        }
	                        insertCommentDataCallBackFun(insertCommentParam,function(insertParam){
		                    	insertData('t_study_comment', insertParam, function(ret, err){
				                
		                            if (ret.status) {
		                               
		                            }
		                            else{
		                             
		                            }
		                             j++;
		                            if(j == dataArr.length){
				                        // loadData(page);
				                        setPageContentFun(dataArr,'false');
				                    }
		                        });
		                    });
		                }
                    }
	            // }
            // })
		} 
	    // 首次加载评论列表
	    //// 服务器请求数据
	 //    api.showProgress({
		// 	style : 'default',
		// 	animationType : 'fade',
		// 	title : '加载中...',
		// 	modal : false
		// });
	    
	    commentListFun = function(){
	    	var postParam={
	            'jid' : $api.getStorage('jid'),
       
	            'page' : page,
	            'pagerows' : pagerow,
	            'uid' : $api.getStorage('uid'),
	            'cid' :getParam.cid,
	             'title':getParam.description,
	             'source' :getParam.source
	        }
	        api.ajax({
	            url: Study_CommentList,
	            method: 'post',
	            timeout: 30,
	            dataType: 'json',
	            returnAll:false,
	            data:{
	                values: postParam
	            }
	        },function(ret,err){
	        	api.hideProgress();
	        	api.refreshHeaderLoadDone();//停止刷新控件
	        	
	            if (ret) {
	                // TODO 
//	          
	                insertCommentData(ret.list);
	                if(page == 1){
                    	loadDataFailShowPageFun(ret.list);
                    }
	            } else {
	              
	                loadData();
	            };
	        });
	    }
        loadRefreshData = function(){
        	// $(".commentListUl").find("li").remove();
			page = 1;
			var connectionType = api.connectionType;
			if(connectionType != 'none'){
				openDB(function(ret, err){
	                if (ret.status) {
	                    var deleteSql = 'delete from t_study_comment';
	                    executeSQL(deleteSql,function(ret, err){
	                        if(ret.status){
	                            commentListFun();
	                        }
	                        else{

	                        }
	                    });
	                }
	            });
			}
			else{
				commentListFun();
			}
			// commentListFun();
		}
		loadMoreRefreshData = function(){
			page++;
			commentListFun();
		}
		// loadRefreshData();
		// setRefreshHeader();
	 // 	setPullUpRefresh();
        // 
        // 
        //查询该课程下评论总数量
        selectCommAllNum = function(){
        	openDB(function(ret, err){
	            if (ret.status) {
	                var selectScoreSql = 'select * from t_study_comment where cid='+getParam.cid;
	                selectSQL(selectScoreSql,function(ret, err){
	                    if(ret.status){
	                        var data = ret.data;
	                     
	                        $api.text($api.dom('#commAllNum'), data.length);
	                        //commentcount
	                        
//	                       var updateSql = "UPDATE t_study_course SET commentcount=" + data.length + " WHERE courseid=" + getParam.cid +  ";";
//	                       alert('11111111'+updateSql);
//	                        executeSQL(updateSql, function(ret, err){
//								  
//						if (ret.status) {
//									alert(JSON.stringify(ret));
//						}else{
//								alert(JSON.stringify(err));
//						}
//					});
	                    }
	                    else{
	                        alert('查询失败'+err.msg);
	                    }
	               
	                });
	            }
	        });
        } 
        // selectCommAllNum();
        
    
        // 接收通知，用于添加发表
        api.addEventListener({
            name: 'loadCommentListData'
        }, function(ret, err){
            // $(".commentListUl").find("li").remove();
            // page = 1;
            // loadData(page);
            // selectCommAllNum();
            loadRefreshData();
        });
        // 接收通知，用于移除页面数据
        api.addEventListener({
            name: 'removeComment'
        }, function(ret, err){
        	
            var deleteCommentId = ret.value.commentId;
    
            // 删除t_study_comment表中该条数据
            openDB(function(ret, err){
                if (ret.status) {
                    var deleteSql = 'delete from t_study_comment where commentid='+deleteCommentId;
                    executeSQL(deleteSql,function(ret, err){
                        if(ret.status){
                            // 删除成功后，同时删除列表中的数据
                            var currentDeleteCommentLiId='#'+deleteCommentId;
                            $('.commentListUl '+currentDeleteCommentLiId).remove();
                        }
                        else{
                            alert('删除失败'+err.msg);
                        }
          
                                selectCommAllNum();
                     
                    });
                }
            });
        });
        //下拉刷新
		function setRefreshHeader() {
			api.setRefreshHeaderInfo({
				visible: true,
		        bgColor: '#ccc',
		        textColor: '#fff',
		        textDown: '下拉刷新...',
		        textUp: '松开刷新...',
		        showTime: true
			}, function(ret, err){
				loadRefreshData();
			});
		}
		// 上拉加载
		function setPullUpRefresh() {
			api.addEventListener({
			    name: 'scrolltobottom'
			}, function(ret, err){
				loadMoreRefreshData();
			});
		}
	}
	    
</script>
</html>
